const { config } = require('process')
const SCLtest = require('../models/SCLtestmodel')

//提交测试结果
exports.SCL90_test = async (req, res) => {
    try{
        if(req.body.type == "scl"){
            var scl= {
                person_id: req.body.id,
                option: req.body.test,
                time: req.body.time
            }
            await SCLtest.create(scl)
            res.status(200).json({
                message: "success"
            })
            }        
        }
    catch(err){
        // console.log(err)
        res.status(400).json({
            message: err
        })
    }
}

//查询所有测试结果
exports.SCL90_history = async (req, res) => {
    try{
        if(req.body.type == "scl"){
            var history= await SCLtest.find({person_id: req.body.id}, {test_id: 1, time: 1,_id:0})
            res.status(200).json({
                message: "success",
                history
            })
        }
    }
    catch(err){
        res.status(400).json({
            message: err
        })
    }
}

//测试结果详情
exports.SCL90_result = async (req, res) => {
    try{
        var origin =await SCLtest.findOne({test_id: req.body.result_id},{option:1,_id:0})
        var option = origin.option
        var result = new Array(10)
        option = option.map(Number)
        var a = option[0] + option[1]
        result[0] = (option[0] + option[3] + option[11] +option[26] + option[39] + option[41] + option[47]
            + option[48] + option[51] + option[52] + option[55] + option[57])/12
        result[1] = (option[2] + option[8] + option[9] + option[27] + option[37] + option[44] + option[50]
            + option[54] + option[64])/10
        result[2] = (option[5] + option[20] + option[33] + option[35] + option[36] + option[40] + option[60]
            + option[68] + option[72])/9
        result[3] = (option[4] + option[13] + option[14] + option[19] + option[21] + option[25] + option[28]
            + option[29] + option[30] + option[31] + option[53] + option[70] + option[78])/13
        result[4] = (option[1] + option[16] + option[22] + option[32] + option[38] + option[56] + option[71]
            + option[77] + option[79] + option[85])/10
        result[5] = (option[10] + option[23] + option[62] + option[66] + option[73] + option[80])/6
        result[6] = (option[12] + option[24] + option[46] + option[49] + option[69] + option[74] + option[81])/7
        result[7] = (option[7] + option[17] + option[42] + option[67] + option[75] + option[82])/6
        result[8] = (option[6] + option[15] + option[34] + option[61] + option[76] + option[83] + option[83]
             + option[84] + option[86] + option[87] + option[89])/10
        result[9] = (option[18] + option[43] + option[58] + option[59] + option[63] + option[65] + option[88])/7
        // console.log(result)
        var status=new Array(3)
        status[0]=result.reduce((total,currentValue)=>{
            {return total+(currentValue>=3?1:0)}},0)
        status[1]=result.reduce((total,currentValue)=>{
            {return total+(currentValue<3&&currentValue>=2?1:0)}},0)
        status[2]=result.reduce((total,currentValue)=>{
            {return total+(currentValue<2?1:0)}},0)
        res.status(200).json({
            message:"success",
            result,
            status
        })
    }
    catch(err){
        // console.log(err)
        res.status(400).json({
            message:err
        })
    }
}
